package com.situ.spring.proxy;

import lombok.Value;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {
    public static Object getProxyFactory(Object target) {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println(target.getClass().getName() + "." + method.getName());
                        System.out.println("开启事务");
                        Object returnValue = method.invoke(target,args);
                        System.out.println("关闭事务");
                        return  returnValue;
                    }
                }
        );
    }
}
